package com.example.yuejian11.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Space;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian11.R;
import com.example.yuejian11.bean.ActivityListEntity;
import com.example.yuejian11.bean.BannerEntity;
import com.example.yuejian11.bean.LoginEntity;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;
import java.util.TooManyListenersException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;

public class LoginActivity extends BaseActivity {

    //绑定组件，相当于find id
    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    FButton login;
    @BindView(R.id.remeber)
    Switch remeber;

    @BindView(R.id.register)
    TextView register;
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);
        initDate();

    }

    void initDate(){
        //设置用户名密码，记住 的数据
        //SPUtils.getInstance().getString("username"); 获取本地存的数据，键为username

        username.setText(SPUtils.getInstance().getString("username"));
        password.setText(SPUtils.getInstance().getString("password"));
        remeber.setChecked(SPUtils.getInstance().getBoolean("isrember"));
    }
    //@OnClick 相当于 setOnClick
    @OnClick(R.id.login)
    void loginMe() {

        //获取输入的用户名密码
        final String un = username.getText().toString();
        final String pwd = password.getText().toString();

        if (StringUtils.isEmpty(un)) {
            ToastUtils.showShort("用户名不能为空");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            ToastUtils.showShort("密码不能为空");
            return;
        }
        //Easyhttp 封装了retrofit2  ，
        EasyHttp.get("yuejian6/public/index.php/json/index/index")
                .params("name", un)
                .params("password", pwd)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        LoginEntity loginEntity = null;

                        try{
                            //将json数据解析成java对象
                            loginEntity =  JSON.parseObject(data, LoginEntity.class);
                        }catch (Exception e ){
                            ToastUtils.showShort("获取数据失败");
                            return;
                        }

                        if (!"0".equals(loginEntity.getError())) {
                            ToastUtils.showShort("登录失败");
                            return;
                        }
                        //已经点击记住密码
                        if (remeber.isChecked()){
                            //将用户名密码存到本地
                            SPUtils.getInstance().put("username", un);
                            SPUtils.getInstance().put("password", pwd);
                            SPUtils.getInstance().put("isrember",remeber.isChecked());
                        }else{
                            //没有记住密码，删除数据
                            SPUtils.getInstance().remove("username");
                            SPUtils.getInstance().remove("password");
                            SPUtils.getInstance().remove("isrember");
                        }
                        //存用户id
                        SPUtils.getInstance().put("uid", loginEntity.getUid());
                        Log.d(TAG, "onSuccess: " + loginEntity);

                        //跳转活动
                        ActivityUtils.startActivity(MainActivity.class);

                        /*
                        Intent intent = new Intent(this,MainActivity.clas);
                        startActivity(intent);
                        */

                        //Toast.makeText(this,"xxxx",Toast.LENGTH_SHORT).show();
                        ToastUtils.showShort("登陆成功");
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    @OnClick(R.id.register)
    void registerMe(){
        ActivityUtils.startActivity(RegisterActivity.class);
    }

}
